package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Region;
import com.abaaba.project2.bean.RegionExample;
import com.abaaba.project2.bean.vo.RegionChildrenVo;
import com.abaaba.project2.bean.vo.RegionChildrensVo;
import com.abaaba.project2.bean.vo.RegionVo;
import com.abaaba.project2.mapper.RegionMapper;
import com.abaaba.project2.service.AdminRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class AdminRegionServiceImpl implements AdminRegionService {

    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<RegionVo> getList() {
        RegionExample regionExample = new RegionExample();
        List<Region> regions = regionMapper.selectByExample(regionExample);
        List<RegionVo> regionVos = new LinkedList<>();
        for (Region region : regions) {
            if (region.getPid() == 0 && region.getType() == 1) {
                RegionVo regionVo = new RegionVo(region.getId(), region.getName(), region.getType(),region.getCode(),  new LinkedList<RegionChildrenVo>());
                regionVos.add(regionVo);
            }
        }

        for (Region region : regions) {
            for (RegionVo  r1 : regionVos) {
                if (region.getPid() == r1.getId() && region.getType() == 2){
                    List<RegionChildrenVo> children = r1.getChildren();
                    RegionChildrenVo regionChildrenVo = new RegionChildrenVo(region.getId(), region.getName(), region.getType(), region.getCode(), new LinkedList<RegionChildrensVo>());
                    children.add(regionChildrenVo);
                }
            }
        }

        for (Region region : regions){
            for (RegionVo  r1 : regionVos) {
                for (RegionChildrenVo r2 : r1.getChildren()) {
                    if (region.getType() == 3 && ((region.getCode() < ((r2.getCode() * 100) +99))
                        && ((region.getCode() > (r2.getCode() * 100))))){
                        List<RegionChildrensVo> children = r2.getChildren();
                        RegionChildrensVo regionChildrensVo = new RegionChildrensVo(region.getId(), region.getName(), region.getType(), region.getCode());
                        children.add(regionChildrensVo);
                    }
                }
            }
        }

        return regionVos;
    }
}
